package com.jinly.ryder.deploy.web;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinly.ryder.account.web.utils.AuthUtil;
import com.jinly.ryder.common.auth.LoginContent;
import com.jinly.ryder.common.auth.model.AuthRole;
import com.jinly.ryder.common.auth.model.AuthType;
import com.jinly.ryder.deploy.service.DeployRecordService;
import com.jinly.ryder.deploy.service.DeployService;
import com.jinly.ryder.deploy.web.mapping.DeployRecordVOMapping;
import com.jinly.ryder.deploy.web.vo.DeployRecordVO;
import com.jinly.ryder.manage.service.AppInstanceService;
import com.jinly.ryder.manage.service.DeployGroupService;
import com.jinly.ryder.manage.service.model.AppInstance;
import com.jinly.ryder.manage.service.model.DeployGroup;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("deploy")
@RequiredArgsConstructor
public class DeployController {

    private final DeployService deployService;
    private final DeployRecordService deployRecordService;
    private final DeployGroupService deployGroupService;
    private final AppInstanceService appInstanceService;
    private final DeployRecordVOMapping deployRecordVOMapping;

    /**
     * 根据分组发布
     *
     * @param deployGroupId
     */
    @GetMapping("deployByGroup")
    public void deployByGroup(Integer deployGroupId) {
        DeployGroup deployGroup = deployGroupService.get(deployGroupId);
        Assert.notNull(deployGroup, "部署组不存在");
        AuthUtil.check(AuthType.APP, deployGroup.getAppName(), AuthRole.DEVELOPER);
        Long operator = LoginContent.getUserId();
        deployService.deployByGroup(deployGroup, operator);
    }

    /**
     * 根据实例发布
     *
     * @param appInstanceId
     */
    @GetMapping("deployByInstance")
    public void deployByInstance(Integer appInstanceId) {
        AppInstance appInstance = appInstanceService.get(appInstanceId);
        Assert.notNull(appInstance, "实例不存在");
        AuthUtil.check(AuthType.APP, appInstance.getAppName(), AuthRole.DEVELOPER);
        Long operator = LoginContent.getUserId();
        deployService.deployByInstance(appInstance, operator);
    }

    /**
     * 根据实例重启
     *
     * @param appInstanceId
     */
    @GetMapping("restartByInstance")
    public void restartByInstance(Integer appInstanceId) {
        AppInstance appInstance = appInstanceService.get(appInstanceId);
        Assert.notNull(appInstance, "实例不存在");
        AuthUtil.check(AuthType.APP, appInstance.getAppName(), AuthRole.DEVELOPER);
        Long operator = LoginContent.getUserId();
        deployService.restartByInstance(appInstance, operator);
    }


    @GetMapping("deployRecordPage")
    public Page<DeployRecordVO> deployRecordPage(Long current, Long size, Integer instanceId) {
        AppInstance appInstance = appInstanceService.get(instanceId);
        Assert.notNull(appInstance, "实例不存在");
        AuthUtil.check(AuthType.APP, appInstance.getAppName(), AuthRole.DEVELOPER);
        return deployRecordVOMapping.toVOPage(deployRecordService.queryRecordPage(new Page(current, size), instanceId));
    }

    @GetMapping("getDeployRecord")
    public DeployRecordVO getDeployRecord(Long recordId) {
        return deployRecordVOMapping.toVo(deployRecordService.getRecord(recordId));
    }


    @GetMapping("lastRecordByGroup")
    public DeployRecordVO getDeployRecord(Integer deployGroupId) {
        DeployGroup deployGroup = deployGroupService.get(deployGroupId);
        Assert.notNull(deployGroup, "部署组不存在");
        AuthUtil.check(AuthType.APP, deployGroup.getAppName(), AuthRole.DEVELOPER);
        return deployRecordVOMapping.toVo(deployRecordService.lastRecordByApplyId(deployGroup.getApplyId()));
    }
}
